package alo.p7.work;

import org.junit.Test;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @ClassName HeapStack
 * @Description 用堆实现队列
 * @Author Fanxc
 * @DATE 2021/3/10 16:25
 * @Version 1.0
 */
public class HeapQueue {
    class Compare{
        private int c;
        private int data;
        Compare(int c,int data){
            this.c=c;
            this.data=data;
        }
    }
    PriorityQueue<Compare> queue=new PriorityQueue<>(
            new Comparator<Compare>() {
                public int compare(Compare c1, Compare c2) {
                return c1.c - c2.c;
                }
            });

    int c=Integer.MIN_VALUE;//优先级
    //入队列
    void push(int element){
        queue.offer(new Compare(c++,element));
    }
    //出队列
    int pop(){
        return queue.poll().data;
    }
    //查看队列口元素
    int top(){
        return queue.peek().data;
    }
    //查看队列的大小
    int size(){
        return queue.size();
    }
    //查看队列是否为空
    boolean isEmpty(){
        return queue.isEmpty();
    }
    @Test
    public void test(){
        HeapQueue hs=new HeapQueue();
        hs.push(1);
        hs.push(2);
        System.out.println(hs.pop());//1
        System.out.println(hs.top());//2
    }

}
